//======================================================================
/// COPYRIGHT MorningCoreTech CO.,LTD
/// @file        SRP_PUCCH_FE_fix.cpp
///
/// @brief       This function performs PUCCH FE Proc
/// @author      cuiyuping
/// @note        
/// @par History
/// -
///       Date|       Author|        Version|        DESCRIPTION
/// ----------|-------------|---------------|-------------------
/// 2022-10-20|   cuiyuping |          0.0.1|            created
//======================================================================a branch
//******************************************************************************/

#include "ue_tx_pucch_fe_fix.h"
#include "ue_tx_pucch_dmrs_gen_fix.h"


void SRP_PUCCH_FE_fix(IN stULParamTable *SimParam,
	IN int8        *BRP_UCI_Op, 
	IN  cmplx_frN  *PUCCH_DMRS_Ip[PUCCH_DMRS_MAX_NUM],
	IN  cmplx_frN  *PUCCH_ZC_Data[PUCCH_DATA_MAX_NUM],
	OUT cmplx_frN *SubFrame_FD_Buffer[MAXSYMNUM])
{

	char cFileAddr[256] = { '\0' };
	/*variables definition*/
	int8   l;
	int8       *PUCCH_ScramblOp;
	cmplx_frN  *PUCCH_QAM_Op;
    cmplx_frN  *PUCCH_Data_Op[PUCCH_DATA_MAX_NUM];
    cmplx_frN  *PUCCH_DFT_Op[PUCCH_DATA_MAX_NUM];
    cmplx_frN  *PUCCH_DMRS_Op[PUCCH_DMRS_MAX_NUM];

	cmplx_t *FreqData;
	double Scale;
	int ScNum = SC_PER_RB * SimParam->PUCCH_RB_Num;
	for (l = 0; l < PUCCH_DATA_MAX_NUM; l++)
	{
		PUCCH_Data_Op[l] = (cmplx_frN *)malloc((PUCCH_Max_RE) * sizeof(cmplx_frN));
		memset(PUCCH_Data_Op[l], 0, (PUCCH_Max_RE) * sizeof(cmplx_frN));

		PUCCH_DFT_Op[l] =  (cmplx_frN *)malloc((PUCCH_Max_RE) * sizeof(cmplx_frN));
		memset(PUCCH_DFT_Op[l], 0, (PUCCH_Max_RE) * sizeof(cmplx_frN));
	}
	for (l = 0; l < PUCCH_DMRS_MAX_NUM; l++)
	{
		PUCCH_DMRS_Op[l] = (cmplx_frN *)malloc((PUCCH_Max_RE) * sizeof(cmplx_frN));
		memset(PUCCH_DMRS_Op[l], 0, (PUCCH_Max_RE) * sizeof(cmplx_frN));
	}

	PUCCH_ScramblOp =  (int8 *)malloc((UCI_MAX_BITNUM) * sizeof(int8));
	memset(PUCCH_ScramblOp, 0, (UCI_MAX_BITNUM) * sizeof(int8));

	PUCCH_QAM_Op = (cmplx_frN *)malloc((UCI_MAX_BITNUM) * sizeof(cmplx_frN));
	memset(PUCCH_QAM_Op, 0, (UCI_MAX_BITNUM) * sizeof(cmplx_frN));

	FreqData = (cmplx_t *)malloc((PUCCH_Max_RE) * sizeof(cmplx_t));
	memset(FreqData, 0, (PUCCH_Max_RE) * sizeof(cmplx_t));
	/*Function body*/
	if (PUCCH_FORMAT_0 == SimParam->PUCCH_Format)
	{
		PUCCH_QAMMod_fix(
			SimParam,
			BRP_UCI_Op,
			PUCCH_QAM_Op
		);
	}
	else if (PUCCH_FORMAT_1 == SimParam->PUCCH_Format)
	{
		PUSCHScrambling_fix(
			BRP_UCI_Op,
			SimParam->UCI_Len,
			SimParam->CiniSramForPUCCH,
			PUCCH_ScramblOp,
			SimParam
		);
		PUCCH_QAMMod_fix(
			SimParam,
			PUCCH_ScramblOp,
			PUCCH_QAM_Op
		);
	}


	 vFilePUCCHDataScrambPrint(SimParam,BRP_UCI_Op,PUCCH_ScramblOp);
	 sprintf(cFileAddr, "Result/PUCCHTestData/CASE%d/PUCCH_QAM_Out.dat", CASEIDX_PRINT);
	 vFilePUCCHDataQAMPrint(cFileAddr,SimParam,PUCCH_QAM_Op);	//

//=====================Block-wise spreading====================//
	PUCCH_Spreading_fix(
		SimParam,
		PUCCH_QAM_Op,
		PUCCH_ZC_Data,
		PUCCH_Data_Op
		); 
	sprintf(cFileAddr, "Result/PUCCHTestData/CASE%d/PUCCH_Data_Spread_Out.dat", CASEIDX_PRINT);
	vFilePUCCHDataSpreadPrint(cFileAddr, SimParam,PUCCH_Data_Op);	//

//=====================Transform precoding====================//
	PUCCH_TransPrecoding_fix(SimParam,PUCCH_Data_Op,PUCCH_DFT_Op);
	sprintf(cFileAddr, "Result/PUCCHTestData/CASE%d/PUCCH_PrecodingOut.dat", CASEIDX_PRINT);
	vFilePUCCHDataDFTPrint(cFileAddr,SimParam,PUCCH_DFT_Op);//

//=====================DMRS Processing ====================//
	PUCCH_DMRS_Gen_fix(SimParam,PUCCH_DMRS_Ip, PUCCH_DMRS_Op);
	sprintf(cFileAddr, "Result/PUCCHTestData/CASE%d/PUCCH_DMRS_Out.dat", CASEIDX_PRINT);
	vFilePUCCHDmrsPrint(cFileAddr,SimParam, PUCCH_DMRS_Op);//


//=====================Mapping to Physical resource ====================//
	PUCCH_RE_Mapper_fix(SimParam, PUCCH_DMRS_Op,PUCCH_DFT_Op,SubFrame_FD_Buffer);
	sprintf(cFileAddr, "Result/PUCCHTestData/CASE%d/PUCCH_ReMapOut.dat", CASEIDX_PRINT);
	vFilePUCCHReMapperPrint(cFileAddr,SimParam,SubFrame_FD_Buffer);//


	char cFileAddr_re[256] = { '\0' };
	char cFileAddr_im[256] = { '\0' };
	sprintf(cFileAddr_re, "Result/PUCCHTestData/CASE%d/FreqData_WX_400M_PUCCH_Fix_re.dat", CASEIDX_PRINT);
	sprintf(cFileAddr_im, "Result/PUCCHTestData/CASE%d/FreqData_WX_400M_PUCCH_Fix_im.dat", CASEIDX_PRINT);

	if (PUCCH_FORMAT_0 == SimParam->PUCCH_Format)
	{
		Scale = 8192.0;
	}
	else
	{
		Scale = SimParam->float_fix_dif;
	}
	for (l = 0; l < MAXSYMNUM; l++)
	{
		for (int k = 0; k < PUCCH_Max_RE; k++)
		{
			FreqData[k].re = SubFrame_FD_Buffer[l][k].re / Scale;
			FreqData[k].im = SubFrame_FD_Buffer[l][k].im / Scale;
		}
		vFilePrintDataFixpQI_WX_Flt(cFileAddr_re, cFileAddr_im, FreqData, PUCCH_Max_RE);
	}


	free(PUCCH_ScramblOp);
	free(PUCCH_QAM_Op);

	for (l = 0; l < PUCCH_DATA_MAX_NUM; l++)
	{
		free(PUCCH_Data_Op[l]);
		free(PUCCH_DFT_Op[l]);
	}
	for (l = 0; l < PUCCH_DMRS_MAX_NUM; l++)
	{
		free(PUCCH_DMRS_Op[l]);
	}
}




